package LeetCode._01_树;

public class _404_左叶子之和 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int sum = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        traverse(root);
        return sum;
    }

    void traverse(TreeNode root){
        if(root == null) return;

        //左叶子节点的三种情况，递归去找子树的左叶子节点
        if(root.left!=null && root.left.left==null && root.left.right==null){
            sum += root.left.val;
        }

        traverse(root.left);
        traverse(root.right);
    }
}
